package com.future.mayi.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;

import butterknife.BindView;
import butterknife.OnClick;

public class FeedbackActivity extends BaseTitleActivity {
    @BindView(R.id.edt_content)
    EditText edtContent;
    @BindView(R.id.tv_enter)
    TextView tvEnter;
    @BindView(R.id.tv_num)
    TextView tvNum;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("意见反馈");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_feedback);
    }

    @Override
    protected void initView() {
        edtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int length = 0;
                if (!TextUtils.isEmpty(edtContent.getText())) {
                    length = edtContent.getText().length();
                }
                tvNum.setText(String.valueOf(length).concat("/200"));
                if (length >= 200) {
                    tvNum.setTextColor(getResources().getColor(R.color.actionsheet_red));
                } else {
                    tvNum.setTextColor(getResources().getColor(R.color.sub_text_color));
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initDates() {


    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        tvEnter.setEnabled(false);
        if (TextUtils.isEmpty(edtContent.getText())){
            toast("反馈意见不能为空");
            tvEnter.setEnabled(true);
            return;
        }
        showLoadingDialog();
        new HttpRequest<DefaultResponse>()
                .with(this)
                .setApiCode(ApiCst.addOpinion)
                .addParam("token", App.getToken(mContext))
                .addParam("content", edtContent.getText().toString())
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        dissmissLoadingDialog();
                        toast("提交成功，谢谢您的反馈");
                        finish();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        dissmissLoadingDialog();
                        toast(message);
                        tvEnter.setEnabled(true);
                    }
                })
                .start(new DefaultResponse());

    }
}
